#include "head.h"
//客户端gets处理逻辑

bool my_gets(sendMsg msg,client_status clients,int net_fd)
{
    printf("msg.token:%s\n",msg.token);
    printf("my_gets客户端进入处理.\n");
    //1.发mode进入相应的函数
    msg.mode=4;
    //又开始拼路径
    char full_path[255]={0};
    strcat(full_path,clients.path);
    strcat(full_path,clients.path_last);
    printf("full_path:%s\n",full_path);
    strcpy(msg.msg,full_path);
    send(net_fd,&msg,sizeof(msg),MSG_NOSIGNAL);

    //获取文件名
    char *next;
    char *last_token;
    char *token=strtok_r(clients.path_last,"/",&next);
    while(token!=NULL){
        last_token=token;
        token=strtok_r(NULL,"/",&next);
    }

    //接收文件前先判断
    recvMsg ret_msg;
    bzero(&msg,sizeof(msg));

    //接收错误信号
    recv(net_fd,&ret_msg,sizeof(ret_msg),MSG_WAITALL);
    if(strcmp(ret_msg.statu,"nopath")==0){
        printf("路径错误\n");
        close(net_fd);
        return false;
    }



    char file_name[1000];
    strcpy(file_name,last_token);

    //检查本地是否有该文件
    int ishere=0;
    char curr_path[1024]={0};
    getcwd(curr_path,sizeof(curr_path));
    DIR *pdir=opendir(curr_path);
    struct dirent *dirent;
    while((dirent=readdir(pdir))!=NULL){
        char *name=dirent->d_name;
        if(strcmp(name,file_name)==0){
            ishere=1;
            break;
            //目录下有相同文件
        }
    }
    closedir(pdir);

    printf("file_name%s\n",file_name);

    //打开或创建文件
    int file_fd=open(file_name,O_RDWR|O_CREAT|O_APPEND,0666);
    off_t offset=0;
    //文件大小，也就是已经读取的大小，后面继续写入的起点     
    offset=lseek(file_fd,0,SEEK_END);
    printf("本地已有文件的:offset:%ld\n",offset);
    //重置偏移量为4096的倍数，也就是mmap页的倍数
    //offset=(offset/4096)*4096;
    //发送偏移量给服务器
    int ret = send(net_fd,&offset,sizeof(offset),MSG_NOSIGNAL);
    printf("offset = %ld ret = %d\n ", offset, ret);


    // 接收真实文件大小
    off_t file_size;
    int retii=recv(net_fd, &file_size, sizeof(file_size), MSG_WAITALL);
    //ERROR_CHECK(ret,-1,"ret_file_size");
    printf("真file_size: %ld   ret: %d\n", file_size,retii);



    //文件调整需要的大小
    //开启mmap映射
    char *p = (char *)mmap(NULL, file_size, PROT_READ|PROT_WRITE, MAP_SHARED, file_fd, 0);
    if (p == MAP_FAILED) { // 错误处理
        perror("mmap");
        close(file_fd);
        return false;
    }
    ftruncate(file_fd, file_size);
    //接收文件
    while (offset < file_size) {
        sleep(0.01);
        //已接收文件大小<文件大小时循环读取 
        int recv_size = file_size-offset<4096 ? file_size-offset : 4096;
        ftruncate(file_fd, offset+recv_size);
        long ret_recv = recv(net_fd, p+offset, recv_size, MSG_WAITALL);
        ERROR_CHECK(ret_recv,-1,"ret_recv_file");
        printf("ret_recv:%ld\n",ret_recv);
        // printf("offset%ld\n", offset);
        offset += ret_recv;
        printf("已接收文件数据: %ld/%ld \n", offset, file_size);
    }
    printf("end_offset:%ld\n",offset);
    //截断成文件当前大小
    //lseek(file_fd,0,SEEK_SET);
    //offset=lseek(file_fd,0,SEEK_END);
    //lseek(file_fd,0,SEEK_SET);
    //ftruncate(file_fd, offset);
    munmap(p, file_size);


    close(file_fd);
    close(net_fd);
    printf("my_gets完毕\n");
    return 0;


}
